import React, {useEffect, useState} from "react";
import {useSelector,useDispatch} from "react-redux";
import api from "../../../api";
import SearchListView from "../SearchListView";
import LoadMore from "../../../components/LoadMore";

//搜索页面处理数据的
const SearchList=(props)=>{

    const [searchData,setSearchData]=useState([])
    const [hasMore,setHasMore]=useState(false)

    useEffect(()=>{
      http()


    },[props.search])

     //重新加载数据
    function OnloadMore(){
     http()
    }


    //定义一个公共的方便 上拉加载更多的时候调用
    function http(){
        console.log(props.search)
        api.search({
            //参数的名称search可以自己定义
            search:props.search
        }).then(res=>{
            if (res.data.status===200){
                //合并数据和以前的数据合并在一起
                setSearchData(searchData.concat(res.data.result.data) )
                setHasMore(res.data.result.hasMore)
            }
        })
    }


    return (
        <div>
            {
                searchData.length>0?
                    <SearchListView search={searchData}/>:
                    <div>正在加载中</div>
            }
            {
                 hasMore?
                     <LoadMore/>:
                     <div>数据正在补货中，请稍后在试........</div>
            }

            <LoadMore OnloadMore={OnloadMore}/>
        </div>
    )
}
export default SearchList